//
//  HTLongGoodsVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTLongGoodsVC.h"
#import "HTLongGoodsCell.h"
#import "HTDeliverGoodsViewController.h"
#import "HTDeliverDetialVC.h"

@interface HTLongGoodsVC ()<UITableViewDelegate,UITableViewDataSource,HTLongGoodsCellDelete>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation HTLongGoodsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    
}
- (void)initData{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT + kHTToolBarHeight + IPHONE_NAVIGATIONBAR_HEIGHT; self.tableView.backgroundColor = kBackgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTLongGoodsCell" bundle:nil] forCellReuseIdentifier:LongGoodsCell];
    self.tableView.width = kHTScreenWidth;
    [self configTableHeader];
    self.tableView.mj_header = self.header;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"你尚未保存任何常发货源";
   
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}
#pragma mark --- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTLongGoodsCell *cell = (HTLongGoodsCell *)[tableView dequeueReusableCellWithIdentifier:LongGoodsCell forIndexPath:indexPath withController:self];
    cell.deliverGoodsModel = self.dataSource[indexPath.row];
    cell.delegete = self;
    cell.refreshBlock = ^{
        [tableView.mj_header beginRefreshing];
    };
    return cell;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    HTDeliverDetialVC *deliver = [[HTDeliverDetialVC alloc]init];
//     deliver.isClosed = NO;
//     deliver.deliverGoodsModel = self.dataSource[indexPath.row];
//    [self.navigationController pushViewController:deliver animated:YES];
//}
- (void)didClickEditGoods:(HTDeliverGoodsModel *)model{
    if ([self.parentViewController isKindOfClass:[HTDeliverGoodsViewController class]]) {
        HTDeliverGoodsViewController *parentVC = (HTDeliverGoodsViewController *) self.parentViewController;
        parentVC.orderid = model.orderid;
        //手动调用
        parentVC.segmentControl.selectedSegmentIndex = 0;
        [parentVC segmentedControlAction:parentVC.segmentControl];
    }
}
#pragma mark --- 网络请求
- (void)loadNewData{
    
    [self netLoadData];
    
}
- (void)netLoadData{
    [HTHttpRequestManager requestDeliverGoodsFrequencyWithSuccess:^(id  _Nullable responseObject) {
       [self.tableView.mj_header endRefreshing];
        DBLog(@"发货列表：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"获取列表失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:@"获取列表失败"];
            return;
        }
        self.dataSource = [HTDeliverGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
         [self.tableView.mj_header endRefreshing];
    }];
}


@end
